var runningTodos = [];

window.addEventListener( 'load', main, false );

function selectedListID( )
{
	return $('tdt-todo-list').value;
}

function selectedTodoID( )
{
	return $('tdt-tree-todo').contentView.getItemAtIndex($('tdt-tree-todo').currentIndex).getAttribute( 'todoID' );
}

function main( )
{
	runningTodos = window.arguments[0].runningTodos;
	tdt_loadTodoListMenu( );
	tdt_loadTodos( );
	$('tdt-new-list').addEventListener( 'click', tdt_newList, false );
	$('tdt-new-todo').addEventListener( 'click', tdt_newTodo, false );

	$('tdt-todo-list').addEventListener( 'select', function(){ tdt_loadTodos( ); }, false );

	$('tdt-tree-todo').addEventListener( 'select', tdt_todoOnclick, false );
	$('tdt-tree-todo').addEventListener( 'dblclick', tdt_todoOnDblclick, false );
	$('tdt-tree-todo').addEventListener( 'context', tdt_todoOnContext, false );
	
	$('tdt-todo-contextmenu-edit').addEventListener( 'click', tdt_editTodo, false );
	$('tdt-todo-contextmenu-delete').addEventListener( 'click', tdt_deleteTodo, false );
}

function tdt_selectTodo( )
{
	Log.debug( 'Selected todo id: '+ selectedTodoID( ) );
	if( selectedTodoID( ) )
	{
		window.arguments[0].todoID = selectedTodoID( );
		return true;
	}
	Log.debug( 'No todo was selected.' );
	return false;
}

function tdt_newTodo( )
{
	Log.debug( 'Trying to get a new todo' );
	var currentTodoListID = selectedListID( );
	if( typeof currentTodoListID != 'undefined' && currentTodoListID != '' && currentTodoListID > 0 )
	{
		Log.debug( 'Trying harder to get a new todo, huh' );
		var win = window.openDialog( "chrome://todotime/content/addTodoDialog.xul",
			"tdt-new-todo", 
			"chrome,resizable,modal,centerscreen",
			{listID : currentTodoListID } //send the current listID
			);
		//tdt_loadTodoListMenu( );
		tdt_loadTodos( currentTodoListID );
		return;
	}
	alert( 'You haven`t selected a Todo List to put the new one in.' )
	Log.debug( 'No list selected for new todo' );
}

function tdt_newList( )
{
	var listTitle = prompt( 'To Do List name' );
	if( listTitle != null )
	{
		listTitle = trim( listTitle );
		if( listTitle == '' )
		{
			alert( 'No blanks.' );
			return false;
		}
		else
		{
			//finally
			TodoList( null, listTitle ).saveToFile( );
			//reload menu
			tdt_loadTodoListMenu( );
			//tdt_loadTodos( $('tdt-todo-list').value );
		}
	}
}

function tdt_loadTodoListMenu( menu )
{
	if( typeof menu == 'undefined' ) { menu = $('tdt-todo-list'); }
	//clear the list
	try {
		removeChildren( menu );
	} catch( e ){ Log.debug( e ) };

	//preserve the selected list
	var selIndex = ( menu.selectedIndex > -1 ? menu.selectedIndex : 0 );
	
	var menulist = create_element( 'menupopup' );
	menu.appendChild( menulist );
	var todoLists = tdt_getTodoLists( );
	for( var i = 0; i < todoLists.length; i++ )
	{
		var list = todoLists[i];
		menulist.appendChild( create_element( 'menuitem', {
			//selected : ( i == 0 ? "true" : "false" ),
			value : list[0],
			label : list[1]
			} )
		);
	}

	menu.selectedIndex = selIndex;
	Log.debug( todoLists.length + ' Todo Lists loaded.' );
}

function tdt_loadTodos( listID, tree )
{
	
	if( typeof listID == 'undefined' ) { listID = selectedListID( ) }
	if( typeof tree == 'undefined' ) { tree = $('tdt-todos'); }
	//clear the list
	try {
		removeChildren( tree );
	} catch( e ){ Log.debug( e ) };

	var todos = tdt_getTodos( listID );
//	Log.dump( todos );
	for( var i in todos )
	{
		var todo = todos[i];
		//Log.debug( todo );
		tree.appendChild(  create_element( 'treeitem', {
				todoID : todo.ID
//				context : "todo-treechildren-contextmenu"
			},
			create_element( 'treerow', {}, 
				create_element( 'treecell', { label : todo.title } ),
				create_element( 'treecell', { label : Time.toDate( todo.startTime ) } ),
				create_element( 'treecell', { label : ( ! in_array(  todo.ID, runningTodos ) ? Time.toHMS( todo.duration ) : "running..." ) } )
				)
			)
		);
	}

	Log.debug( todos.length + ' Todos for todolist:'+ listID +' loaded.' );
}

function tdt_todoOnclick( event )
{
	var todoItem = event.target;
//	Log.debug( event.target );
	//left button clicked
//	if( event.button == 0 )
//	{
		Log.debug( "clicked todo "+ todoItem.getAttribute( 'todoID' ) );
		window.arguments[0].selected={todoID: todoItem.getAttribute( 'todoID' )};
//	}
//	else
//	{
//		tdt_todoOnContext( event );
//	}
}

function tdt_todoOnDblclick( event )
{
	var todoItem = event.target;
	window.arguments[0].selected.todoID=todoItem.getAttribute( 'todoID' );
	tdt_selectTodo( );
	window.close( );
}

function tdt_todoOnContext( event )
{
	Log.debug( "tdt_todoOnContext( ) executing" );
	var todoItem = event.target;
	
	//var tree = $("tdt-tree-todo");
	//document.popupNode = tree;
	//$("tdt-tree-todo").showPopup( $('tdt-todo-contextmenu'), event.screenX, event.screenY, "popup", "topleft", "bottomleft" );
}

function tdt_editTodo( event )
{
	var todoID = selectedTodoID( );
//	Log.debug( todoItem.nodeName );
	Log.debug( 'Trying to edit a todo' );
	var currentTodoListID = selectedListID( );
	if( typeof currentTodoListID != 'undefined' && currentTodoListID != '' && currentTodoListID > 0 )
	{
		Log.debug( 'Trying harder to edit a todo: '+ todoID );
		var win = window.openDialog( "chrome://todotime/content/addTodoDialog.xul",
			"tdt-new-todo", 
			"chrome,resizable,modal,centerscreen",
			{listID : currentTodoListID, selectedTodoID : todoID } //send the current listID
			);
		//tdt_loadTodoListMenu( );
		tdt_loadTodos( currentTodoListID );
		return;
	}
	alert( 'You haven`t selected a Todo List to put the new one in.' )
	Log.debug( 'No list selected for new todo' );
}

function tdt_deleteTodo( event )
{
	var todoID = selectedTodoID( );
	tdt_deleteTodoById( todoID );
	//reload todos
	tdt_loadTodos( );
}